package com.choosefine.it.worker.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.worker.model.WorkerItemsRelation;
import com.choosefine.it.worker.model.dto.WorkerItemsRelationDTO;
import com.choosefine.it.worker.model.dto.WorkerItemsRelationFindDTO;
import com.choosefine.it.worker.model.dto.WorkerItemsRelationUpdateDTO;
import com.choosefine.it.worker.model.vo.WorkerItemsRelationFindVO;
import com.choosefine.it.worker.model.vo.WorkerItemsRelationVO;
import com.choosefine.it.worker.service.MyWorksManagerService;
import com.choosefine.it.worker.service.WorkerItemsRelationService;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * @author chenjian
 * @date 2016-12-16 下午8:02:32
 */
@RestController
@RequestMapping("/worker/workerItemsRelation")
@Api(value="工人与班组工作关系",description="陈健")
public class WorkerItemsRelationController extends BaseController{
	
	@Autowired
	WorkerItemsRelationService workerItemRelationService;
	
	@Autowired
	MyWorksManagerService myWorksManagerService;
	
	/**
	 * @author chenjian
	 * @time 2016年12月24日上午11:53:09
	 */
	@ApiOperation(value="删除工人与班组关系(陈健)",notes="控制台-项目管理-项目基本资料")
	@ApiImplicitParam(name="relationId",value="关系id",required=true,dataType="int",paramType="path")
	@RequestMapping(value="/deleteWorkerItemsRelation/{relationId}",method=RequestMethod.PUT,produces="application/json")
	public String deleteWorkerItemsRelation(@PathVariable Integer relationId){
		String result=null;
		if(relationId!=null){
			WorkerItemsRelation workerItemsRelation = new WorkerItemsRelation();
			workerItemsRelation.setUtime(DateUtils.getNowTime());
			workerItemsRelation.setIsValid(CommonConstant.INVALID);
			workerItemsRelation.setId(relationId);
			int issuccess=workerItemRelationService.deleteWorkerItemsRelation(workerItemsRelation);
			if(issuccess>0){
				result=responseSuccess(issuccess);
			}else{
				result=responseFail(ResultCode.MSG_UPDATE_FAILD);
			}
		}else{
			result=responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}
	
	/**
	 * @author chenjian
	 * @time 2016-12-16 下午7:16:26
	 */
	@ApiOperation(value="根据条件查询工人信息列表(陈健)",notes="控制台-项目管理-项目基本资料",response = WorkerItemsRelationVO.class)
	@RequestMapping(value="/findWorkerByTeam",method=RequestMethod.POST,produces="application/json")
	public String findWorkerByTeam(@RequestBody WorkerItemsRelationFindDTO workerItemsRelationFindDTO){
		String result=null;
		if(workerItemsRelationFindDTO.getTeamId()!=null){
			Map<String, Object> params = new HashMap<>();
			params.put("teamId", workerItemsRelationFindDTO.getTeamId());
			params.put("isValid", CommonConstant.VALID);
			if(StringUtils.isNotBlank(workerItemsRelationFindDTO.getUserCode())){
				params.put("userCode",workerItemsRelationFindDTO.getUserCode().trim());
			}
			if(StringUtils.isNotBlank(workerItemsRelationFindDTO.getName())){
				params.put("name",workerItemsRelationFindDTO.getName().trim());
			}
			if(StringUtils.isNotBlank(workerItemsRelationFindDTO.getCardId())){
				params.put("cardId",workerItemsRelationFindDTO.getCardId().trim());
			}
			if(workerItemsRelationFindDTO.getWorkType()!=null&&workerItemsRelationFindDTO.getWorkType()!=0){
				params.put("workType", workerItemsRelationFindDTO.getWorkType());
			}
			PageDTO pageDTO = new PageDTO();
			pageDTO.setPageNum(workerItemsRelationFindDTO.getPageNum());
			pageDTO.setPageSize(workerItemsRelationFindDTO.getPageSize());
			Page<WorkerItemsRelationVO>  list=workerItemRelationService.findWorkerByTeam(pageDTO,params);
			if(list.size()>0){
				result = responseArraySuccess(list.toPageInfo());
			}else{
				result = responseFail(ResultCode.MSG_DATA_ISEMPTY);
			}
		}else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	/**
	 * 保存工人与班组的关系
	 * @author chenjian
	 * @time 2016-12-21 下午8:47:25
	 */
	@ApiOperation(value="保存工人与班组的关系(陈健)",notes="控制台-项目管理-项目基本资料")
	@RequestMapping(value="/saveWorkItemsRelation",method=RequestMethod.POST)
	public String saveWorkItemsRelation(@RequestBody WorkerItemsRelationDTO workerItemsRelationDTO){
		String result=null;
		WorkerItemsRelation workerItemsRelation = new WorkerItemsRelation();
		workerItemsRelation.setCtime(DateUtils.getNowTime());
		workerItemsRelation.setIsValid(CommonConstant.VALID);
		workerItemsRelation.setProjectCode(workerItemsRelationDTO.getProjectCode());
		workerItemsRelation.setTeamId(workerItemsRelationDTO.getTeamId());
		workerItemsRelation.setUserCode(workerItemsRelationDTO.getUserCode());
		workerItemsRelation.setWorkerTypeIdes(workerItemsRelationDTO.getWorkerTypeIdes());
		workerItemsRelation.setWorkTime(DateUtils.getNowTime());
		int isSuccess=0;
		try {
			isSuccess = myWorksManagerService.addBatchWorkerItemsRelation(Arrays.asList(workerItemsRelation));
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		if(isSuccess>0){
			result=responseSuccess(isSuccess, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		}else {
			result=responseFail();
		}
		return result;
	}
	
	/**
	 * @Description：修改工人信息
	 * @Author：chenjian
	 * @Create Date：2017年2月16日
	 * @param：
	 * @return：
	 */
	@ApiOperation(value="修改工人信息(陈健)",notes="控制台-项目管理-项目基本资料")
	@RequestMapping(value="/updateWorker",method=RequestMethod.POST)
	public String updateWorker(@RequestBody WorkerItemsRelationUpdateDTO workerItemsRelationUpdateDTO){
		String result=null;
//		WorkerItemsRelation workerItemsRelation = new WorkerItemsRelation();
//		workerItemsRelation.setUtime(DateUtils.getNowTime());
//		workerItemsRelation.setWorkTime(workerItemsRelationUpdateDTO.getWorkTime());
//		workerItemsRelation.setWorkerTypeIdes(workerItemsRelationUpdateDTO.getWorkerTypeIdes());
//		workerItemsRelation.setId(workerItemsRelationUpdateDTO.getId());
		int	isSuccess = workerItemRelationService.updateWorkerTypes(workerItemsRelationUpdateDTO);
		if(isSuccess>0){
			result=responseSuccess(isSuccess, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		}else {
			result=responseFail();
		}
		return result;
	}
	
	/**
	 * 集合保存工人与班组的关系
	 * @author chenjian
	 * @time 2016年12月31日下午3:21:20
	 */
	@ApiOperation(value="批量保存工人与班组的关系(陈健)",notes="控制台-项目管理-项目基本资料")
	@RequestMapping(value="/workItemsRelationList",method=RequestMethod.POST)
	public String saveWorkItemsRelationList(@RequestBody List<WorkerItemsRelation> workerItemsRelationList){
		String result=null;
		int isSuccess=0;
		try {
			isSuccess = myWorksManagerService.addBatchWorkerItemsRelation(workerItemsRelationList);
		} catch (BusinessException e) {
			e.printStackTrace();
			result=responseFail();
		}
		if(isSuccess>0){
			result=responseSuccess(isSuccess, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		}else {
			result=responseFail();
		}
		return result;
	}
	
	/**
	 * 根据工人租户编号和班组id查询班组成员
	 * @author chenjian
	 * @time 2016年12月31日下午3:30:05
	 */
	@ApiOperation(value="根据工人租户编号和班组id查询班组成员(陈健)",notes="控制台-项目管理-项目基本资料")
	@ApiImplicitParams({
		@ApiImplicitParam(name="userCode",value="工人租户编号",dataType="String",paramType="query",required=true),
		@ApiImplicitParam(name="teamId",value="班组id",dataType="Integer",paramType="query",required=true)
	})
	@RequestMapping(value="/memberByUserCodeAndTeamId",method=RequestMethod.GET)
	public String findMemberByUserCodeAndTeamId(@RequestParam String userCode,@RequestParam Integer teamId){
		String result=null;
		int isSuccess=0;
		try {
			Map<String, Object> params=new HashMap<>();
			params.put("userCode", userCode);
			params.put("teamId", teamId);
			isSuccess = workerItemRelationService.findMemberByUserCodeAndTeamId(params);
		} catch (Exception e) {
			e.printStackTrace();
			result=responseFail();
		}
		if(isSuccess>0){
			result=responseSuccess(isSuccess, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		}else {
			result=responseFail(ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
	}
	@ApiOperation(value="根据项目的code和班组的ID查询关系（潘钱钦）",notes="自有材料设备接口用")
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目code",dataType="String",paramType="query",required=true),
		@ApiImplicitParam(name="teamid",value="班组id",dataType="Integer",paramType="query",required=true)
	})
	@RequestMapping(value="/selectByProjectCodeAndTeamId",method=RequestMethod.GET)
	public String selectByProjectCodeAndTeamId(@RequestParam String projectCode,@RequestParam Integer teamid){
		Integer relation=null;
		if(projectCode!=null&&teamid!=null){
			Map<String, Object> params =new HashMap<>();
			params.put("projectCode", projectCode);
			params.put("teamid", teamid);
			relation=workerItemRelationService.selectByProjectCodeAndTeamId(params);
		}
		return responseSuccess(relation);
	}
	
	/**
	 * @Description：根据班组工人关系id查询工人信息（陈健）
	 * @Author：chenjian
	 * @Create Date：2017年2月23日
	 * @param：
	 * @return：
	 */
	@ApiOperation(value="根据班组工人关系id查询工人信息（陈健）",notes="控制台-项目基本信息工人信息编辑",response=WorkerItemsRelationFindVO.class)
	@ApiImplicitParam(name="id",value="班组工人关系id",dataType="Integer",paramType="path",required=true)
	@RequestMapping(value="/findWorker/{id}",method=RequestMethod.GET)
	public String findWorker(@PathVariable Integer id){
		if(id!=null&&id.intValue()>0){
			WorkerItemsRelationFindVO workerItemsRelationFindVO=null;
			workerItemsRelationFindVO = workerItemRelationService.findWorker(id);
			return responseSuccess(workerItemsRelationFindVO);
		}else{
			return responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
	}
}